package com.framework.common.jdbc.interceptor;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/1/25 10:39
 * @since JDK 1.8
 */
@Intercepts(value = {
        @Signature(type=Executor.class,
                method="query",
                args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class,
                        CacheKey.class,BoundSql.class}),
        @Signature(type=Executor.class,
                method="query",
                args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})})
//@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class MysqlPagingInterceptor extends PagingInterceptor {

    @Override
    protected String getPagingSQL(PageParaBean page, String originalSql) {
        int offset = (page.getPage() - 1) * page.getRows();
        offset = offset < 0 ? 0 : offset;
        StringBuffer sb = new StringBuffer(originalSql).append(" limit ").append(offset).append(",").append(page.getRows());
        return sb.toString();
    }
    private static final Logger LOGGER = LoggerFactory.getLogger(MysqlPagingInterceptor.class);


}